package com.czhoujunlv.drug;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.os.ConditionVariable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.czhoujunlv.drug.Bean.RegisterData;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Register extends AppCompatActivity {

    EditText id,username,password,verification;
    ImageView code;
    ConstraintLayout register,back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
        Glide.with(Register.this)
                .load("http://192.168.43.138:8080/index/code?num=1")
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(code);
        //刷新验证码
        code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Glide.with(Register.this)
                        .load("http://192.168.43.138:8080/index/code?num=1")
                        .skipMemoryCache(true)//清除缓存，实现刷新
                        .diskCacheStrategy(DiskCacheStrategy.NONE)//取消磁盘缓存
                        .into(code);
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterData registerData = new RegisterData();
                registerData.setAccount(id.getText().toString());
                registerData.setPass(password.getText().toString());
                registerData.setCode(verification.getText().toString());
                registerData.setCodeId("1");
                registerData.setName(username.getText().toString());
                Gson gson = new Gson();
                final String json = gson.toJson(registerData);
                Thread thread = new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            Log.i("注册的json：",json);
                            post("http://192.168.43.138:8080/index/consumer/registerCon",json);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                };
                thread.start();
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    private void init(){
        id = findViewById(R.id.editText2);
        username = findViewById(R.id.editText5);
        password =findViewById(R.id.editText);
        verification = findViewById(R.id.editText3);
        register = findViewById(R.id.constraintLayout18);
        back = findViewById(R.id.constraintLayout19);
        code = findViewById(R.id.imageView20);
    }
    private void post(String url,String json) throws IOException{
        RequestBody Body = RequestBody.create(MediaType.parse("application/json"), json);
        Request request = new Request.Builder()
                .url(url)
                .post(Body)
                .build();
        Response response = MainActivity.client.newCall(request).execute();
        if (response.isSuccessful()) {
            String res = response.body().string();
            Log.i("返回注册结果：", res);
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }
}